package com.tanhua.server.controller;

import cn.hutool.core.util.StrUtil;
import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 发布小视频
     *
     * @param picFile
     * @param videoFile
     * @return
     */
    @PostMapping
    public Object saveVideo(@RequestParam("videoThumbnail") MultipartFile picFile,
                            @RequestParam("videoFile") MultipartFile videoFile) {
        return this.videoService.saveVideo(picFile, videoFile);
    }

    /**
     * 查询小视频列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public PageResult queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return this.videoService.queryVideoList(Math.max(page, 1), pageSize);
    }

    @PostMapping("/{id}/like")
    public Object likeVideo(@PathVariable("id") String vedioId) {
        return this.videoService.likeVideo(vedioId);
    }

    @PostMapping("/{id}/dislike")
    public Object disLikeVideo(@PathVariable("id") String vedioId) {
        return this.videoService.disLikeVideo(vedioId);
    }

    @GetMapping("/{id}/comments")
    public PageResult getCommentList(@PathVariable("id") String videoId,
                                     @RequestParam(value = "page", defaultValue = "1") String page,
                                     @RequestParam(value = "pageSize", defaultValue = "10") String pageSize) {
        return this.videoService.getCommentList(videoId, page, pageSize);
    }

    @PostMapping("/{id}/comments")
    public Boolean saveComment(@RequestBody Map<String, String> param) {
        String videoId = param.get("id");
        String comment = param.get("comment");
        if(StrUtil.isAllNotEmpty(videoId, comment)) {
            return this.videoService.saveComment(videoId, comment);
        }
        return false;
    }

    @PostMapping("/comments/{id}/like")
    public Object likeComment(@PathVariable("id") String commentId) {
        return this.videoService.likeVideo(commentId);
    }

    @PostMapping("comments/{id}/dislike")
    public Object disLikeComment(@PathVariable("id") String commentId) {
        return this.videoService.disLikeVideo(commentId);
    }

    @PostMapping("/{uid}/userFocus")
    public Object userFocus(@PathVariable("uid") String uid) {
        return this.videoService.userFocus(uid);
    }

    @PostMapping("/{uid}/userUnFocus")
    public Object userUnFocus(@PathVariable("uid") String uid) {
        return this.videoService.userUnFocus(uid);
    }
}
